<template>
  <div>
    <!-- 头部 -->
    <div class="header">
      <div style="width: 30%; display: flex; align-items: center">
        <div style="width: 120px">商品名称：</div>
        <el-input v-model="pageData.name" />
      </div>
      <div class="buttons">
        <el-button @click="getList">搜索</el-button>
        <el-button @click="reSet">重置</el-button>
        <el-button @click="$router.push('/productManager/addDetail')"
          >新增</el-button
        >
      </div>
    </div>
    <!-- 表格 -->
    <el-table max-height="520" :data="list" border style="width: 100%">
      <el-table-column fixed type="index" label="序号" width="100" />
      <el-table-column fixed prop="pic_url" label="商品图片" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.pic_url" alt="" width="70" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="240" prop="name" />
      <el-table-column label="商品价格" width="170" prop="price" />
      <el-table-column label="商品类别" width="180" prop="tags" />
      <el-table-column label="品牌" width="180" prop="brand" />
      <el-table-column label="商品库存" width="150" prop="stock" />
      <el-table-column label="小标签" width="200" prop="tags" />
      <!-- 操作 -->
      <el-table-column fixed="right" label="操作" width="130">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="$router.push(`/productManager/addDetail?id=${scope.row.id}`)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="$router.push(`/productManager/addDetail?id=${scope.row.id}&type=1`)">详情</el-button>
          <el-button
            type="text"
            size="small"
            style="color: red"
            @click="deleThis(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="foot">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageData.page"
        :page-sizes="[10, 15, 20]"
        :page-size="pageData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getGoodsList, deleteGoods } from "@/api/goods";
export default {
  name: "ProductList",
  data() {
    return {
      list: [], //表格数据
      pageData: {
        limit: 10,
        page: 1,
        name: "",
      },
      count: 0, //总条数
    };
  },
  methods: {
    //获取表格数据
    async getList() {
      let res = await getGoodsList(this.pageData);
      let { data } = res;
      this.list = data.list;
      this.count = data.count;
    },
    //重置
    reSet() {
      this.pageData.name = "";
      this.getList();
    },
    //每页条数变化
    handleSizeChange(size) {
      this.pageData.limit = size;
      this.getList();
    },
    //当前页发生变化
    handleCurrentChange(page) {
      this.pageData.page = page;
      this.getList();
    },
    //删除数据
    deleThis(id) {
      this.$confirm("此操作将永久删除该轮商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await deleteGoods({id})
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style scoped lang="scss">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px;
}
.foot {
  margin-top: 10px;
  float: right;
}
</style>